<?php
namespace Api\Controller;

use Api\Model\LoginModel;

class UserController extends BaseController
{


    /**
     * 修改用户位置
     */
    public function changeLocaltion()
    {
        $phone=I('post.phone');//手机号码
        $key=I('post.key');  //登录凭证
        $lng=I('post.lng'); //经度
        $lat=I('post.lat'); //纬度
        if(empty($phone)){
            $data=array('err_code'=>1001,'err_msg'=>'手机号码不能为空');
            $this->ajaxReturn($data,'JSON');
        }

        if(!preg_match("/^1[3-9][0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$phone)){
            //不验证通过
            $data=array('err_code'=>1002,'err_msg'=>'请正确输入手机号码');
            $this->ajaxReturn($data,'JSON');
        }

        if(empty($lng) || empty($lat)){
            $data=array('err_code'=>1001,'err_msg'=>'经纬度不能为空');
            $this->ajaxReturn($data,'JSON');
        }
       /* if(!verifyKey($phone,$key)){
            $data=array(code=>1003,msg=>'登录超时');
            $this->ajaxReturn($data,'JSON');
        }*/
        $where['phone']=$phone;
        $map['lng']=$lng;
        $map['lat']=$lat;
        if(M('user')->where($where)->save($map)===false){
            $data=array('err_code'=>403,'err_msg'=>'修改失败');
            $this->ajaxReturn($data,'JSON');
        }else{
            $data=array('err_code'=>200,'err_msg'=>'修改成功');
            $this->ajaxReturn($data,'JSON');
        }
    }

    /**
     * 修改jpush别名
     */
    public function changepush(){
        //解决跨域
        echoHeader();
        $user_id = I('post.user_id','');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $push_name = I('post.push_name','');
        $data['user_id'] = $user_id;
        $data['push_name'] = $push_name;
        $info = M('user')->save($data);
        if($info){
            $data=array('err_code'=>200, 'err_msg'=>'修改成功');
            $this->ajaxReturn($data,'JSON');
        }else{
            $data=array('err_code'=>403, 'err_msg'=>'修改失败');
            $this->ajaxReturn($data,'JSON');
        }
    }

    /**
     * 查找经纬度
     */

    public function getlola(){
        //解决跨域
        echoHeader();
        $order_id = I('post.order_id');
        $info = M('repair_order')->field('user_id,worker_user_id,repair_order_id')->where("repair_order_num_alias = $order_id")->find();
        $data['user'] = M('user')->field('lng,lat')->where("user_id = $info[user_id]")->find();
        $data['worker'] = M('user')->field('lng,lat')->where("user_id = $info[worker_user_id]")->find();
        if(empty($data['user']) || empty($data['worker']) ){
            $info = array('err_code'=>404,'err_msg'=>'暂时没有数据');
            $this->ajaxReturn($info);
        }else{
            $info = array('err_code'=>200,'err_msg'=>$data);
            $this->ajaxReturn($info);
        }

    }

    /**
     * 个人中心
     * @param user_id 用户id
     */

    public function UserInfo(){
        $user_id = I('user_id');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        if(IS_POST){
            $user_id = I('post.user_id');
            $maps['user_name'] = trim(I('post.user_name'));
            $maps['gender'] = trim(I('post.gender')); //男女枚举
            $maps['age'] = trim(I('post.age'));
            $sav['user_id'] = array('eq',$user_id);
            $info = M('user')->where($sav)->save($maps);
            if($info){
                $array = array('err_code'=>200,'err_msg'=>'修改成功');
            }else{
                $array = array('err_code'=>200,'err_msg'=>'没有修改');
            }
            $this->ajaxReturn($array);
        }
        //获取用户信息
        $info = M('user')->field('user_name,user_face,gender,age,phone')->where("user_id = $user_id")->find();
        $array = array('err_code'=>200,'err_msg'=>$info);
        $this->ajaxReturn($array);
    }

    /**
     * 查询师傅名字
     * @param user_id
     */

    public function getMasterName()
    {
        $user_id = I('user_id');
        $data = self::checkUserIsExist($user_id);
        if (!$data['user']) {
            $this->ajaxReturn($data);
        }
        if($data['user']['identity']==1){
            $Info = M('User')->field('user_name')->where("user_id = $user_id")->find();
            $array = array('err_code'=>200,'err_msg'=>$Info['user_name']);
        }else{
            if($data['user']['is_real']!=1){
                $array = array('err_code'=>403,'err_msg'=>'未认证或审核未通过');
                $this->ajaxReturn($array);
            }
            $User  = M('UserRealInfo')->where("user_id = $user_id")->find();
            $array = array('err_code'=>200,'err_msg'=>$User['id_name']);
        }
        $this->ajaxReturn($array);

    }

    public function deluser(){
        M('user')->where("phone = 15815800670")->delete();
    }

}
